// pages/quickWeather/quickWeather.js
Page({

  data: {
    toDayWeather: {
      temperature: "0/0",
      weather_state: "晴",
      air: "90",
      wind_power: "4级",
      humidity: "61",
      pressure: "963"
    },
    futureWeather: [
      {
        date: "请先选择城市",
        temperature: "请先选择城市",
        weather_state: "请先选择城市",
      },
      {
        date: "请先选择城市",
        temperature: "请先选择城市",
        weather_state: "请先选择城市",
      },
      {
        date: "请先选择城市",
        temperature: "请先选择城市",
        weather_state: "请先选择城市",
      }
    ],
    city:getApp().globalData.city
  },

  // 加载页面完成后，绑定数据
  onShow: function () {
    console.log("show")
    var app = getApp()
    this.getWeatherData(app.globalData.city)
    this.setData({
      city:app.globalData.city
    })
  },
  //获取天气数据
  getWeatherData: function (city) {
    var that = this
    var url = 'http://apis.juhe.cn/simpleWeather/query?city=' + city + '&key=4332df0a4724f00d6666cf66990778e7'
    wx.request({
      url: url,
      data: {},
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res)
        that.getWeather(res)
        that.getFutureWeather(res)
      }
    })
  },
  // 解析当天天气
  getWeather: function (res) {
    var weather = {
      temperature: "",
      weather_state: "",
      air: "",
      wind_power: "",
      humidity: "",
      pressure: ""
    }
    var data = res.data.result.realtime
    console.log(data)
    weather.temperature = data.temperature
    weather.weather_state = data.info
    weather.air = data.aqi
    weather.wind_power = data.power
    weather.humidity = data.humidity
    weather.pressure = "965"
    this.setData({
      toDayWeather: weather
    })

  },

  // 解析未来3天天气
  getFutureWeather: function (res) {
    var futureWeatherData = [
      {
        date: "",
        temperature: "",
        weather_state: "",
      },
      {
        date: "",
        temperature: "",
        weather_state: "",
      },
      {
        date: "",
        temperature: "",
        weather_state: "",
      }
    ]
    var futureData = res.data.result.future
    console.log(futureData)
    for (var i = 1; i <= 3; i++) {
      var date = futureData[i].date.split("-")
      futureWeatherData[i - 1].date = date[1] + "/" + date[2]
      futureWeatherData[i - 1].temperature = futureData[i].temperature
      futureWeatherData[i - 1].weather_state = futureData[i].weather

      console.log("==========================")
      console.log(futureWeatherData[i - 1].date)
      console.log(futureWeatherData[i - 1].temperature)
      console.log(futureWeatherData[i - 1].weather_state)
    }
    this.setData({
      futureWeather: futureWeatherData
    })

  },

  //跳转到天气页
  goToCity: function (e) {
    wx.navigateTo({
      url: '../citys/citys',
    })
  }


})